package com.lichuang.dedao.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lichuang.dedao.user.entity.History;
import com.lichuang.dedao.user.enums.CollectionType;
import com.lichuang.dedao.user.mapper.HistoryMapper;
import com.lichuang.dedao.user.service.IHistoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 历史记录 服务实现类
 * </p>
 *
 * @author hanghang
 * @since 2019-07-14
 */
@Service
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, History> implements IHistoryService {

    @Override
    @CachePut(cacheNames = "history", key = "#userId", unless = "#result == null")
    public List<History> addHistory(Integer userId, CollectionType collectionType, Integer objectId, String objectName) {
        History history = new History();
        history.setUserId(userId)
                .setType(collectionType.getValue())
                .setObjectId(objectId)
                .setObjectName(objectName)
                .setReadTime(LocalDateTime.now());
        baseMapper.insert(history);
        History historyParam = new History();
        historyParam.setUserId(userId);
        QueryWrapper queryWrapper = new QueryWrapper(historyParam);
        List<History> histories = baseMapper.selectList(queryWrapper);
        return histories;
    }

    @Override
    @Cacheable(cacheNames = "history", key = "#userId", unless = "#result == null")
    public List<History> selectHistories(Integer userId) {
        History historyParam = new History();
        historyParam.setUserId(userId);
        QueryWrapper queryWrapper = new QueryWrapper(historyParam);
        List<History> histories = baseMapper.selectList(queryWrapper);
        return histories;
    }
}
